<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <script>
      /**
       * // Definition for a Node.
       * function Node(val,children) {
       *    this.val = val;
       *    this.children = children;
       * };
       */

      var levelOrder = function (root) {
        if (!root) return []
        function bfs(root) {
          let res = []
          let queue = [root]
          while (queue.length) {
            let len = queue.length
            res.push([])
            for (let i = 0; i < len; i++) {
              let node = queue.shift()
              res[res.length - 1].push(node.val)
              let children = node.children || []
              for (let i = 0; i < children.length; i++) {
                queue.push(children[i])
              }
            }
          }
          return res
        }
        return bfs(root)
      }
    </script>
  </body>
</html>
